In [119]:
# multiple time series analysis
# 20160724, 20160801
# Sean Chang 
In [44]:
import numpy as np
import pandas as pd
import scipy as sp
import os  
import matplotlib.pyplot as plt
%matplotlib inline  
import seaborn as sns
sns.set(style="darkgrid")

from datetime import datetime
import matplotlib.pyplot as plt 
#os.chdir('/home/sc268/Dropbox/codes/2016_timeseries_currency/')
#print os.getcwd()
In [ ]:
## UDFs 

def merge_ts(all_ts):
    all_ts_arr = []
    ts_id = []
    for id in all_ts.keys():
        all_ts_arr.append(all_ts[id])
        ts_id.append(id)

    df = pd.concat(all_ts_arr, join='outer', axis=1)
    df.columns = ts_id
    return df

def to_percentage_change(all_ts):
    normalized_ts = {}
    for id in all_ts.keys():
        ts = all_ts[id]
        normalized_ts[id] = (ts - ts.shift()) / ts
    return normalized_ts


def plot_corr(all_ts):
    # merge time series with shared timestamps (dates)
    df = merge_ts(all_ts)

    corr_mat = df.corr()
    sm.graphics.plot_corr(corr_mat, xnames=df.columns)
    return corr_mat

def clean_up_df(dat):
    print dat.head()
    # delete unnecessary columns
    dat = dat.ix[:, 0:4]
    
    # filter:: only consider 8 am in each day
    dat = dat.loc[[time == 80000 for time in dat['<TIME>']]].reset_index(drop=True)
    
    # create timestamp
    dat['time'] = map(lambda dd, tt: datetime.strptime(str(dd)+str(tt),'%Y%m%d%H%M%S'), dat['<DTYYYYMMDD>'], dat['<TIME>'])
    
    # rename 
    dat.rename(columns={'<TICKER>':'currency', '<OPEN>':'rate'}, inplace=True)
    
    # delete unused columns
    del dat['<DTYYYYMMDD>'], dat['<TIME>']
    
    print dat.head()
    return dat

Data clean up / create sample data

In [ ]:
# data filter
# raw data is from http://www.forextester.com/data/datasources

usdjpy =pd.read_csv('raw_data/USDJPY.txt')
usdjpy = clean_up_df(usdjpy)
#usdjpy.to_csv('new_usdjpy.csv')

import os
foldername = "raw_data/"

for file in os.listdir(foldername):
    filename = os.path.splitext(file)[0]
    print file, filename
    dat = pd.read_csv('raw_data/' + file)
    dat = clean_up_df(dat)
    dat.to_csv('data/' + filename + '.csv')
In [3]:
dat = pd.read_csv('data/EURUSD.csv')
EUR = pd.Series(list(dat.rate), index = dat.time)
EUR.plot()
Out[3]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f03c814ced0>
In [4]:
dat = pd.read_csv('data/AUDUSD.csv')
AUD = pd.Series(list(dat.rate), index = dat.time)
AUD.plot()
Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f03c7d81450>
In [5]:
dat = pd.read_csv('data/GBPUSD.csv')
GBP = pd.Series(list(dat.rate), index = dat.time)
GBP.plot()
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f03c585f310>
In [6]:
dat = pd.read_csv('data/NZDUSD.csv')
NZD= pd.Series(list(dat.rate), index = dat.time)
NZD.plot()
Out[6]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f03c525d450>
In [15]:
dat = pd.read_csv('data/CHFUSD.csv')
#dat.rate = [1.0/rate for rate in dat.rate]
#dat.to_csv('data/CHFUSD.csv', index = False)
CHF= pd.Series(list(dat.rate), index = dat.time)
CHF.plot()
Out[15]:
<matplotlib.axes._subplots.AxesSubplot at 0x1178b0510>
In [14]:
dat = pd.read_csv('data/JPYUSD.csv')
#dat.rate = [1.0/rate for rate in dat.rate]
#dat.to_csv('data/JPYUSD.csv', index = False)
JPY= pd.Series(list(dat.rate), index = dat.time)
JPY.plot()
Out[14]:
<matplotlib.axes._subplots.AxesSubplot at 0x116e50f90>

read sample data

In [32]:
def read_ts(path):
    all_ts = {}
    for file in os.listdir(path):
        if file.split('.')[1] == 'csv':
            tmp = path + '/' + file
            print tmp
            dat = pd.read_csv(path + '/' + file)  # get data
            currency = file[:3]        # get currency
            dat['day'] = pd.to_datetime(dat.time)  # change string to datatime
            dat = dat.set_index('day', drop=True)  # use day as index
            if 'Unnamed: 0' in dat.columns: del dat['Unnamed: 0']
            del dat['time'], dat.index.name, dat['currency']
            all_ts[currency] = dat  # save in a dictionary
    return all_ts
all_ts = read_ts('/Users/schang/Dropbox/codes/2016_timeseries_currency/data')
all_ts
/Users/schang/Dropbox/codes/2016_timeseries_currency/data/AUDUSD.csv
/Users/schang/Dropbox/codes/2016_timeseries_currency/data/CHFUSD.csv
/Users/schang/Dropbox/codes/2016_timeseries_currency/data/EURUSD.csv
/Users/schang/Dropbox/codes/2016_timeseries_currency/data/GBPUSD.csv
/Users/schang/Dropbox/codes/2016_timeseries_currency/data/JPYUSD.csv
/Users/schang/Dropbox/codes/2016_timeseries_currency/data/NZDUSD.csv
Out[32]:
{'AUD':                        rate
 2001-01-03 08:00:00  0.5630
 2001-01-04 08:00:00  0.5603
 2001-01-05 08:00:00  0.5698
 2001-01-08 08:00:00  0.5691
 2001-01-10 08:00:00  0.5578
 2001-01-11 08:00:00  0.5544
 2001-01-12 08:00:00  0.5616
 2001-01-15 08:00:00  0.5514
 2001-01-16 08:00:00  0.5569
 2001-01-17 08:00:00  0.5546
 2001-01-18 08:00:00  0.5560
 2001-01-19 08:00:00  0.5611
 2001-01-22 08:00:00  0.5566
 2001-01-23 08:00:00  0.5556
 2001-01-25 08:00:00  0.5431
 2001-01-26 08:00:00  0.5455
 2001-01-29 08:00:00  0.5432
 2001-01-30 08:00:00  0.5439
 2001-01-31 08:00:00  0.5467
 2001-02-01 08:00:00  0.5504
 2001-02-02 08:00:00  0.5549
 2001-02-05 08:00:00  0.5526
 2001-02-06 08:00:00  0.5502
 2001-02-07 08:00:00  0.5480
 2001-02-08 08:00:00  0.5431
 2001-02-09 08:00:00  0.5358
 2001-02-12 08:00:00  0.5383
 2001-02-13 08:00:00  0.5357
 2001-02-14 08:00:00  0.5300
 2001-02-15 08:00:00  0.5266
 ...                     ...
 2016-05-20 08:00:00  0.7239
 2016-05-23 08:00:00  0.7222
 2016-05-24 08:00:00  0.7156
 2016-05-25 08:00:00  0.7190
 2016-05-26 08:00:00  0.7196
 2016-05-27 08:00:00  0.7222
 2016-05-30 08:00:00  0.7170
 2016-05-31 08:00:00  0.7234
 2016-06-01 08:00:00  0.7259
 2016-06-02 08:00:00  0.7223
 2016-06-03 08:00:00  0.7251
 2016-06-06 08:00:00  0.7354
 2016-06-07 08:00:00  0.7433
 2016-06-08 08:00:00  0.7448
 2016-06-09 08:00:00  0.7437
 2016-06-10 08:00:00  0.7404
 2016-06-13 08:00:00  0.7397
 2016-06-14 08:00:00  0.7364
 2016-06-15 08:00:00  0.7381
 2016-06-16 08:00:00  0.7355
 2016-06-17 08:00:00  0.7389
 2016-06-20 08:00:00  0.7457
 2016-06-21 08:00:00  0.7491
 2016-06-22 08:00:00  0.7482
 2016-06-23 08:00:00  0.7535
 2016-06-24 08:00:00  0.7398
 2016-06-27 08:00:00  0.7445
 2016-06-28 08:00:00  0.7403
 2016-06-29 08:00:00  0.7414
 2016-06-30 08:00:00  0.7426
 
 [3754 rows x 1 columns], 'CHF':                          rate
 2001-01-03 08:00:00  0.623791
 2001-01-04 08:00:00  0.621388
 2001-01-05 08:00:00  0.625117
 2001-01-08 08:00:00  0.624844
 2001-01-09 08:00:00  0.621504
 2001-01-10 08:00:00  0.619003
 2001-01-11 08:00:00  0.615612
 2001-01-12 08:00:00  0.620386
 2001-01-15 08:00:00  0.612933
 2001-01-16 08:00:00  0.611583
 2001-01-17 08:00:00  0.611471
 2001-01-18 08:00:00  0.610091
 2001-01-19 08:00:00  0.620001
 2001-01-22 08:00:00  0.611696
 2001-01-23 08:00:00  0.613271
 2001-01-24 08:00:00  0.611210
 2001-01-25 08:00:00  0.601540
 2001-01-26 08:00:00  0.606171
 2001-01-29 08:00:00  0.605290
 2001-01-30 08:00:00  0.603355
 2001-01-31 08:00:00  0.609236
 2001-02-01 08:00:00  0.613459
 2001-02-02 08:00:00  0.613911
 2001-02-05 08:00:00  0.611845
 2001-02-06 08:00:00  0.610277
 2001-02-07 08:00:00  0.603391
 2001-02-08 08:00:00  0.604851
 2001-02-09 08:00:00  0.599413
 2001-02-12 08:00:00  0.606024
 2001-02-13 08:00:00  0.602736
 ...                       ...
 2016-05-20 08:00:00  1.009285
 2016-05-23 08:00:00  1.010407
 2016-05-24 08:00:00  1.008065
 2016-05-25 08:00:00  1.008166
 2016-05-26 08:00:00  1.008776
 2016-05-27 08:00:00  1.009897
 2016-05-30 08:00:00  1.005834
 2016-05-31 08:00:00  1.007861
 2016-06-01 08:00:00  1.008065
 2016-06-02 08:00:00  1.013377
 2016-06-03 08:00:00  1.009591
 2016-06-06 08:00:00  1.023541
 2016-06-07 08:00:00  1.032738
 2016-06-08 08:00:00  1.036269
 2016-06-09 08:00:00  1.041775
 2016-06-10 08:00:00  1.038206
 2016-06-13 08:00:00  1.037452
 2016-06-14 08:00:00  1.037883
 2016-06-15 08:00:00  1.035947
 2016-06-16 08:00:00  1.040799
 2016-06-17 08:00:00  1.037560
 2016-06-20 08:00:00  1.043950
 2016-06-21 08:00:00  1.041450
 2016-06-22 08:00:00  1.041450
 2016-06-23 08:00:00  1.044605
 2016-06-24 08:00:00  1.028807
 2016-06-27 08:00:00  1.028595
 2016-06-28 08:00:00  1.022181
 2016-06-29 08:00:00  1.020096
 2016-06-30 08:00:00  1.021346
 
 [3968 rows x 1 columns], 'EUR':                        rate
 2001-01-03 08:00:00  0.9490
 2001-01-04 08:00:00  0.9414
 2001-01-05 08:00:00  0.9569
 2001-01-08 08:00:00  0.9562
 2001-01-09 08:00:00  0.9463
 2001-01-10 08:00:00  0.9434
 2001-01-11 08:00:00  0.9407
 2001-01-12 08:00:00  0.9544
 2001-01-15 08:00:00  0.9446
 2001-01-16 08:00:00  0.9423
 2001-01-17 08:00:00  0.9408
 2001-01-18 08:00:00  0.9376
 2001-01-19 08:00:00  0.9477
 2001-01-22 08:00:00  0.9362
 2001-01-23 08:00:00  0.9401
 2001-01-24 08:00:00  0.9352
 2001-01-25 08:00:00  0.9192
 2001-01-26 08:00:00  0.9244
 2001-01-29 08:00:00  0.9243
 2001-01-30 08:00:00  0.9165
 2001-01-31 08:00:00  0.9304
 2001-02-01 08:00:00  0.9402
 2001-02-02 08:00:00  0.9408
 2001-02-05 08:00:00  0.9421
 2001-02-06 08:00:00  0.9373
 2001-02-07 08:00:00  0.9286
 2001-02-08 08:00:00  0.9271
 2001-02-09 08:00:00  0.9193
 2001-02-12 08:00:00  0.9297
 2001-02-13 08:00:00  0.9261
 ...                     ...
 2016-05-20 08:00:00  1.1212
 2016-05-23 08:00:00  1.1220
 2016-05-24 08:00:00  1.1180
 2016-05-25 08:00:00  1.1142
 2016-05-26 08:00:00  1.1169
 2016-05-27 08:00:00  1.1174
 2016-05-30 08:00:00  1.1123
 2016-05-31 08:00:00  1.1127
 2016-06-01 08:00:00  1.1149
 2016-06-02 08:00:00  1.1209
 2016-06-03 08:00:00  1.1149
 2016-06-06 08:00:00  1.1348
 2016-06-07 08:00:00  1.1364
 2016-06-08 08:00:00  1.1367
 2016-06-09 08:00:00  1.1377
 2016-06-10 08:00:00  1.1315
 2016-06-13 08:00:00  1.1266
 2016-06-14 08:00:00  1.1252
 2016-06-15 08:00:00  1.1219
 2016-06-16 08:00:00  1.1269
 2016-06-17 08:00:00  1.1252
 2016-06-20 08:00:00  1.1342
 2016-06-21 08:00:00  1.1337
 2016-06-22 08:00:00  1.1273
 2016-06-23 08:00:00  1.1321
 2016-06-24 08:00:00  1.1164
 2016-06-27 08:00:00  1.1064
 2016-06-28 08:00:00  1.1067
 2016-06-29 08:00:00  1.1073
 2016-06-30 08:00:00  1.1114
 
 [3953 rows x 1 columns], 'GBP':                        rate
 2001-01-03 08:00:00  1.4999
 2001-01-04 08:00:00  1.4978
 2001-01-05 08:00:00  1.5056
 2001-01-08 08:00:00  1.5087
 2001-01-09 08:00:00  1.4946
 2001-01-10 08:00:00  1.4896
 2001-01-11 08:00:00  1.4890
 2001-01-12 08:00:00  1.4999
 2001-01-15 08:00:00  1.4721
 2001-01-16 08:00:00  1.4782
 2001-01-17 08:00:00  1.4695
 2001-01-18 08:00:00  1.4763
 2001-01-19 08:00:00  1.4759
 2001-01-22 08:00:00  1.4652
 2001-01-23 08:00:00  1.4673
 2001-01-24 08:00:00  1.4662
 2001-01-25 08:00:00  1.4527
 2001-01-26 08:00:00  1.4605
 2001-01-29 08:00:00  1.4611
 2001-01-30 08:00:00  1.4567
 2001-01-31 08:00:00  1.4616
 2001-02-01 08:00:00  1.4693
 2001-02-02 08:00:00  1.4821
 2001-02-05 08:00:00  1.4737
 2001-02-06 08:00:00  1.4717
 2001-02-07 08:00:00  1.4579
 2001-02-08 08:00:00  1.4531
 2001-02-09 08:00:00  1.4450
 2001-02-12 08:00:00  1.4516
 2001-02-13 08:00:00  1.4489
 ...                     ...
 2016-05-20 08:00:00  1.4566
 2016-05-23 08:00:00  1.4503
 2016-05-24 08:00:00  1.4541
 2016-05-25 08:00:00  1.4655
 2016-05-26 08:00:00  1.4717
 2016-05-27 08:00:00  1.4675
 2016-05-30 08:00:00  1.4604
 2016-05-31 08:00:00  1.4595
 2016-06-01 08:00:00  1.4485
 2016-06-02 08:00:00  1.4442
 2016-06-03 08:00:00  1.4429
 2016-06-06 08:00:00  1.4422
 2016-06-07 08:00:00  1.4521
 2016-06-08 08:00:00  1.4516
 2016-06-09 08:00:00  1.4460
 2016-06-10 08:00:00  1.4429
 2016-06-13 08:00:00  1.4184
 2016-06-14 08:00:00  1.4138
 2016-06-15 08:00:00  1.4168
 2016-06-16 08:00:00  1.4155
 2016-06-17 08:00:00  1.4274
 2016-06-20 08:00:00  1.4590
 2016-06-21 08:00:00  1.4721
 2016-06-22 08:00:00  1.4657
 2016-06-23 08:00:00  1.4747
 2016-06-24 08:00:00  1.3810
 2016-06-27 08:00:00  1.3407
 2016-06-28 08:00:00  1.3336
 2016-06-29 08:00:00  1.3394
 2016-06-30 08:00:00  1.3468
 
 [3952 rows x 1 columns], 'JPY':                          rate
 2001-01-03 08:00:00  0.008731
 2001-01-04 08:00:00  0.008763
 2001-01-05 08:00:00  0.008589
 2001-01-08 08:00:00  0.008594
 2001-01-09 08:00:00  0.008627
 2001-01-10 08:00:00  0.008583
 2001-01-11 08:00:00  0.008573
 2001-01-12 08:00:00  0.008468
 2001-01-15 08:00:00  0.008405
 2001-01-16 08:00:00  0.008467
 2001-01-17 08:00:00  0.008526
 2001-01-18 08:00:00  0.008398
 2001-01-19 08:00:00  0.008482
 2001-01-22 08:00:00  0.008560
 2001-01-23 08:00:00  0.008542
 2001-01-24 08:00:00  0.008540
 2001-01-25 08:00:00  0.008480
 2001-01-26 08:00:00  0.008568
 2001-01-29 08:00:00  0.008558
 2001-01-30 08:00:00  0.008581
 2001-01-31 08:00:00  0.008596
 2001-02-01 08:00:00  0.008604
 2001-02-02 08:00:00  0.008683
 2001-02-05 08:00:00  0.008639
 2001-02-06 08:00:00  0.008707
 2001-02-07 08:00:00  0.008683
 2001-02-08 08:00:00  0.008599
 2001-02-09 08:00:00  0.008580
 2001-02-12 08:00:00  0.008500
 2001-02-13 08:00:00  0.008536
 ...                       ...
 2016-05-20 08:00:00  0.009070
 2016-05-23 08:00:00  0.009132
 2016-05-24 08:00:00  0.009129
 2016-05-25 08:00:00  0.009089
 2016-05-26 08:00:00  0.009093
 2016-05-27 08:00:00  0.009110
 2016-05-30 08:00:00  0.008982
 2016-05-31 08:00:00  0.008998
 2016-06-01 08:00:00  0.009086
 2016-06-02 08:00:00  0.009179
 2016-06-03 08:00:00  0.009197
 2016-06-06 08:00:00  0.009331
 2016-06-07 08:00:00  0.009284
 2016-06-08 08:00:00  0.009350
 2016-06-09 08:00:00  0.009405
 2016-06-10 08:00:00  0.009361
 2016-06-13 08:00:00  0.009426
 2016-06-14 08:00:00  0.009446
 2016-06-15 08:00:00  0.009414
 2016-06-16 08:00:00  0.009619
 2016-06-17 08:00:00  0.009590
 2016-06-20 08:00:00  0.009568
 2016-06-21 08:00:00  0.009573
 2016-06-22 08:00:00  0.009583
 2016-06-23 08:00:00  0.009577
 2016-06-24 08:00:00  0.009700
 2016-06-27 08:00:00  0.009807
 2016-06-28 08:00:00  0.009784
 2016-06-29 08:00:00  0.009767
 2016-06-30 08:00:00  0.009739
 
 [3942 rows x 1 columns], 'NZD':                        rate
 2003-01-02 08:00:00  0.5233
 2003-01-20 08:00:00  0.5504
 2003-01-23 08:00:00  0.5446
 2003-01-24 08:00:00  0.5517
 2003-01-27 08:00:00  0.5491
 2003-02-04 08:00:00  0.5469
 2003-02-05 08:00:00  0.5500
 2003-02-06 08:00:00  0.5468
 2003-02-10 08:00:00  0.5505
 2003-02-11 08:00:00  0.5477
 2003-02-12 08:00:00  0.5507
 2003-03-12 08:00:00  0.5535
 2003-03-13 08:00:00  0.5457
 2003-03-14 08:00:00  0.5479
 2003-03-17 08:00:00  0.5555
 2003-03-18 08:00:00  0.5481
 2003-03-19 08:00:00  0.5487
 2003-03-20 08:00:00  0.5508
 2003-03-21 08:00:00  0.5527
 2003-03-24 08:00:00  0.5519
 2003-03-26 08:00:00  0.5479
 2003-03-28 08:00:00  0.5497
 2003-03-31 08:00:00  0.5541
 2003-04-03 08:00:00  0.5515
 2003-04-04 08:00:00  0.5435
 2003-04-07 08:00:00  0.5339
 2003-04-08 08:00:00  0.5416
 2003-04-09 08:00:00  0.5467
 2003-04-11 08:00:00  0.5464
 2003-04-15 08:00:00  0.5472
 ...                     ...
 2016-05-20 08:00:00  0.6766
 2016-05-23 08:00:00  0.6789
 2016-05-24 08:00:00  0.6711
 2016-05-25 08:00:00  0.6742
 2016-05-26 08:00:00  0.6711
 2016-05-27 08:00:00  0.6728
 2016-05-30 08:00:00  0.6690
 2016-05-31 08:00:00  0.6713
 2016-06-01 08:00:00  0.6797
 2016-06-02 08:00:00  0.6803
 2016-06-03 08:00:00  0.6836
 2016-06-06 08:00:00  0.6927
 2016-06-07 08:00:00  0.6935
 2016-06-08 08:00:00  0.6985
 2016-06-09 08:00:00  0.7115
 2016-06-10 08:00:00  0.7091
 2016-06-13 08:00:00  0.7058
 2016-06-14 08:00:00  0.7028
 2016-06-15 08:00:00  0.7025
 2016-06-16 08:00:00  0.7038
 2016-06-17 08:00:00  0.7054
 2016-06-20 08:00:00  0.7109
 2016-06-21 08:00:00  0.7134
 2016-06-22 08:00:00  0.7142
 2016-06-23 08:00:00  0.7194
 2016-06-24 08:00:00  0.7056
 2016-06-27 08:00:00  0.7086
 2016-06-28 08:00:00  0.7077
 2016-06-29 08:00:00  0.7088
 2016-06-30 08:00:00  0.7094
 
 [3376 rows x 1 columns]}

Visualization

In [49]:
from bokeh.io import output_notebook
from bokeh.plotting import figure, show
from bokeh.charts import TimeSeries
#from bokeh.models import Range1d
from IPython.display import display
from subprocess import call
TOOLS = "pan, box_zoom, reset, save, box_select"
output_notebook()
Loading BokehJS ...
In [62]:
df = merge_ts(all_ts)
df.plot()
Out[62]:
<matplotlib.axes._subplots.AxesSubplot at 0x11a708110>
In [110]:
all_ts = read_ts('/Users/schang/Dropbox/codes/2016_timeseries_currency/data')
df = merge_ts(all_ts)
df['Date'] = df.index
df = df.reset_index(drop= True)
df.columns
/Users/schang/Dropbox/codes/2016_timeseries_currency/data/AUDUSD.csv
/Users/schang/Dropbox/codes/2016_timeseries_currency/data/CHFUSD.csv
/Users/schang/Dropbox/codes/2016_timeseries_currency/data/EURUSD.csv
/Users/schang/Dropbox/codes/2016_timeseries_currency/data/GBPUSD.csv
/Users/schang/Dropbox/codes/2016_timeseries_currency/data/JPYUSD.csv
/Users/schang/Dropbox/codes/2016_timeseries_currency/data/NZDUSD.csv
Out[110]:
Index([u'AUD', u'CHF', u'JPY', u'GBP', u'NZD', u'EUR', u'Date'], dtype='object')
In [111]:
p = TimeSeries(df, x= 'Date', legend=True,
               title="Exchange rate", ylabel='exchange rates')
show(p)
Out[111]:

<Bokeh Notebook handle for In[111]>

Modelling

In [48]:
# smoother
from statsmodels.nonparametric.smoothers_lowess import lowess

ts = all_ts['JPY']
ts.plot()

def smooth_ts(ts):
    all_ts_smoothed = {} 
    filtered = lowess(list(ts.rate), range(len(ts)), is_sorted=True, frac=0.05, it=0)
    return pd.Series(filtered[:,1], index = ts.index)

ts_smoothed = smooth_ts(ts)
ts_smoothed.plot()
Out[48]:
<matplotlib.axes._subplots.AxesSubplot at 0x11a4931d0>
In [36]:
 
In [38]:
import statsmodels.api as sm

# pair-wise correlation
plot_corr(all_ts)
Out[38]:
AUD CHF JPY GBP NZD EUR
AUD 1.000000 0.835758 0.761035 0.145462 0.855457 0.759437
CHF 0.835758 1.000000 0.653729 -0.063116 0.732875 0.640380
JPY 0.761035 0.653729 1.000000 -0.176444 0.420416 0.546724
GBP 0.145462 -0.063116 -0.176444 1.000000 0.009755 0.545300
NZD 0.855457 0.732875 0.420416 0.009755 1.000000 0.432331
EUR 0.759437 0.640380 0.546724 0.545300 0.432331 1.000000
In [39]:
from statsmodels.tsa.vector_ar import var_model
from statsmodels.nonparametric.smoothers_lowess import lowess

df = merge_ts(all_ts)
model = var_model.VAR(df, missing= 'drop')
res = model.fit(5)
In [40]:
res.summary()
Out[40]:
  Summary of Regression Results   
==================================
Model:                         VAR
Method:                        OLS
Date:           Mon, 01, Aug, 2016
Time:                     17:24:24
--------------------------------------------------------------------
No. of Equations:         6.00000    BIC:                   -70.6532
Nobs:                     3310.00    HQIC:                  -70.8735
Log likelihood:           89504.7    FPE:                1.46792e-31
AIC:                     -70.9963    Det(Omega_mle):     1.38807e-31
--------------------------------------------------------------------
Results for equation AUD
=========================================================================
            coefficient       std. error           t-stat            prob
-------------------------------------------------------------------------
const          0.002320         0.003017            0.769           0.442
L1.AUD         0.921409         0.033702           27.340           0.000
L1.CHF        -0.010641         0.024587           -0.433           0.665
L1.JPY         1.468319         2.010975            0.730           0.465
L1.GBP         0.008601         0.016345            0.526           0.599
L1.NZD         0.008065         0.037119            0.217           0.828
L1.EUR         0.026329         0.025429            1.035           0.301
L2.AUD         0.051845         0.047296            1.096           0.273
L2.CHF         0.021474         0.036553            0.587           0.557
L2.JPY        -4.650091         2.815944           -1.651           0.099
L2.GBP        -0.017688         0.023514           -0.752           0.452
L2.NZD         0.013001         0.052382            0.248           0.804
L2.EUR         0.001906         0.036732            0.052           0.959
L3.AUD         0.025469         0.047335            0.538           0.591
L3.CHF         0.008157         0.036741            0.222           0.824
L3.JPY         5.416604         2.822244            1.919           0.055
L3.GBP         0.003792         0.023572            0.161           0.872
L3.NZD        -0.007024         0.052374           -0.134           0.893
L3.EUR        -0.065332         0.036804           -1.775           0.076
L4.AUD         0.000982         0.047379            0.021           0.983
L4.CHF        -0.074874         0.036433           -2.055           0.040
L4.JPY        -3.315665         2.821864           -1.175           0.240
L4.GBP        -0.018721         0.023574           -0.794           0.427
L4.NZD        -0.080651         0.052340           -1.541           0.123
L4.EUR         0.160164         0.036642            4.371           0.000
L5.AUD        -0.005594         0.033658           -0.166           0.868
L5.CHF         0.053489         0.024477            2.185           0.029
L5.JPY         1.538069         2.019645            0.762           0.446
L5.GBP         0.023191         0.016390            1.415           0.157
L5.NZD         0.068520         0.037074            1.848           0.065
L5.EUR        -0.122698         0.025321           -4.846           0.000
=========================================================================

Results for equation CHF
=========================================================================
            coefficient       std. error           t-stat            prob
-------------------------------------------------------------------------
const          0.012424         0.003075            4.040           0.000
L1.AUD         0.003158         0.034344            0.092           0.927
L1.CHF         1.077411         0.025056           43.000           0.000
L1.JPY        -4.294716         2.049298           -2.096           0.036
L1.GBP        -0.017792         0.016657           -1.068           0.286
L1.NZD        -0.035482         0.037826           -0.938           0.348
L1.EUR         0.010010         0.025914            0.386           0.699
L2.AUD        -0.078882         0.048197           -1.637           0.102
L2.CHF        -0.143561         0.037250           -3.854           0.000
L2.JPY         6.732747         2.869607            2.346           0.019
L2.GBP         0.056867         0.023962            2.373           0.018
L2.NZD         0.092366         0.053380            1.730           0.084
L2.EUR         0.005602         0.037432            0.150           0.881
L3.AUD         0.087473         0.048237            1.813           0.070
L3.CHF         0.035675         0.037441            0.953           0.341
L3.JPY        -0.442898         2.876028           -0.154           0.878
L3.GBP        -0.033959         0.024021           -1.414           0.158
L3.NZD        -0.049847         0.053372           -0.934           0.350
L3.EUR        -0.030509         0.037505           -0.813           0.416
L4.AUD         0.003712         0.048282            0.077           0.939
L4.CHF         0.008612         0.037127            0.232           0.817
L4.JPY        -0.459112         2.875640           -0.160           0.873
L4.GBP        -0.039932         0.024023           -1.662           0.097
L4.NZD        -0.034478         0.053337           -0.646           0.518
L4.EUR         0.087606         0.037340            2.346           0.019
L5.AUD        -0.008311         0.034300           -0.242           0.809
L5.CHF         0.011560         0.024944            0.463           0.643
L5.JPY        -1.842757         2.058133           -0.895           0.371
L5.GBP         0.029379         0.016703            1.759           0.079
L5.NZD         0.030313         0.037781            0.802           0.422
L5.EUR        -0.071531         0.025804           -2.772           0.006
=========================================================================

Results for equation JPY
=========================================================================
            coefficient       std. error           t-stat            prob
-------------------------------------------------------------------------
const          0.000048         0.000028            1.713           0.087
L1.AUD         0.000370         0.000315            1.174           0.240
L1.CHF        -0.000290         0.000230           -1.263           0.207
L1.JPY         0.963775         0.018804           51.255           0.000
L1.GBP        -0.000081         0.000153           -0.529           0.597
L1.NZD        -0.000350         0.000347           -1.007           0.314
L1.EUR         0.000601         0.000238            2.529           0.011
L2.AUD        -0.000788         0.000442           -1.782           0.075
L2.CHF         0.000084         0.000342            0.247           0.805
L2.JPY         0.015724         0.026330            0.597           0.550
L2.GBP         0.000266         0.000220            1.210           0.226
L2.NZD         0.000496         0.000490            1.013           0.311
L2.EUR        -0.000668         0.000343           -1.945           0.052
L3.AUD         0.000673         0.000443            1.521           0.128
L3.CHF         0.000004         0.000344            0.013           0.990
L3.JPY         0.022767         0.026389            0.863           0.388
L3.GBP        -0.000063         0.000220           -0.288           0.773
L3.NZD        -0.000156         0.000490           -0.318           0.750
L3.EUR        -0.000301         0.000344           -0.876           0.381
L4.AUD        -0.000427         0.000443           -0.963           0.336
L4.CHF        -0.000013         0.000341           -0.038           0.969
L4.JPY         0.000942         0.026386            0.036           0.972
L4.GBP         0.000136         0.000220            0.616           0.538
L4.NZD        -0.000106         0.000489           -0.217           0.828
L4.EUR         0.000401         0.000343            1.171           0.242
L5.AUD         0.000195         0.000315            0.620           0.535
L5.CHF         0.000205         0.000229            0.895           0.371
L5.JPY        -0.005648         0.018885           -0.299           0.765
L5.GBP        -0.000273         0.000153           -1.784           0.074
L5.NZD         0.000072         0.000347            0.208           0.836
L5.EUR        -0.000014         0.000237           -0.061           0.951
=========================================================================

Results for equation GBP
=========================================================================
            coefficient       std. error           t-stat            prob
-------------------------------------------------------------------------
const          0.014240         0.004484            3.176           0.002
L1.AUD        -0.028415         0.050084           -0.567           0.571
L1.CHF        -0.012720         0.036539           -0.348           0.728
L1.JPY        -4.915131         2.988472           -1.645           0.100
L1.GBP         1.042718         0.024290           42.928           0.000
L1.NZD        -0.059739         0.055161           -1.083           0.279
L1.EUR         0.043728         0.037789            1.157           0.247
L2.AUD         0.039118         0.070285            0.557           0.578
L2.CHF        -0.003423         0.054321           -0.063           0.950
L2.JPY         3.215002         4.184721            0.768           0.442
L2.GBP        -0.067988         0.034944           -1.946           0.052
L2.NZD         0.075813         0.077844            0.974           0.330
L2.EUR        -0.011066         0.054586           -0.203           0.839
L3.AUD         0.015877         0.070343            0.226           0.821
L3.CHF         0.011020         0.054600            0.202           0.840
L3.JPY         2.219002         4.194084            0.529           0.597
L3.GBP        -0.006218         0.035030           -0.178           0.859
L3.NZD        -0.022253         0.077832           -0.286           0.775
L3.EUR        -0.054145         0.054694           -0.990           0.322
L4.AUD        -0.026415         0.070410           -0.375           0.708
L4.CHF        -0.070109         0.054142           -1.295           0.195
L4.JPY         3.292996         4.193519            0.785           0.432
L4.GBP        -0.007820         0.035033           -0.223           0.823
L4.NZD        -0.120642         0.077781           -1.551           0.121
L4.EUR         0.141291         0.054453            2.595           0.010
L5.AUD         0.009814         0.050019            0.196           0.844
L5.CHF         0.066087         0.036375            1.817           0.069
L5.JPY        -4.181341         3.001356           -1.393           0.164
L5.GBP         0.034137         0.024357            1.402           0.161
L5.NZD         0.125731         0.055095            2.282           0.023
L5.EUR        -0.120512         0.037630           -3.203           0.001
=========================================================================

Results for equation NZD
=========================================================================
            coefficient       std. error           t-stat            prob
-------------------------------------------------------------------------
const          0.003399         0.002665            1.275           0.202
L1.AUD        -0.059090         0.029766           -1.985           0.047
L1.CHF        -0.017956         0.021716           -0.827           0.408
L1.JPY         1.692859         1.776117            0.953           0.341
L1.GBP         0.003288         0.014436            0.228           0.820
L1.NZD         0.995243         0.032784           30.358           0.000
L1.EUR         0.039575         0.022459            1.762           0.078
L2.AUD         0.064117         0.041772            1.535           0.125
L2.CHF         0.016983         0.032284            0.526           0.599
L2.JPY        -4.393727         2.487075           -1.767           0.077
L2.GBP        -0.005503         0.020768           -0.265           0.791
L2.NZD        -0.004871         0.046265           -0.105           0.916
L2.EUR        -0.030398         0.032442           -0.937           0.349
L3.AUD        -0.004043         0.041807           -0.097           0.923
L3.CHF         0.002469         0.032450            0.076           0.939
L3.JPY         1.958963         2.492640            0.786           0.432
L3.GBP        -0.000649         0.020819           -0.031           0.975
L3.NZD         0.026736         0.046258            0.578           0.563
L3.EUR        -0.036412         0.032506           -1.120           0.263
L4.AUD         0.014092         0.041846            0.337           0.736
L4.CHF        -0.069348         0.032178           -2.155           0.031
L4.JPY         0.714397         2.492304            0.287           0.774
L4.GBP        -0.020544         0.020821           -0.987           0.324
L4.NZD        -0.087487         0.046227           -1.893           0.059
L4.EUR         0.123886         0.032363            3.828           0.000
L5.AUD        -0.013266         0.029728           -0.446           0.655
L5.CHF         0.067357         0.021619            3.116           0.002
L5.JPY         0.132195         1.783774            0.074           0.941
L5.GBP         0.023131         0.014476            1.598           0.110
L5.NZD         0.064615         0.032744            1.973           0.049
L5.EUR        -0.097271         0.022364           -4.349           0.000
=========================================================================

Results for equation EUR
=========================================================================
            coefficient       std. error           t-stat            prob
-------------------------------------------------------------------------
const          0.009934         0.003619            2.745           0.006
L1.AUD        -0.081190         0.040425           -2.008           0.045
L1.CHF        -0.027010         0.029492           -0.916           0.360
L1.JPY         1.423900         2.412131            0.590           0.555
L1.GBP         0.009480         0.019606            0.484           0.629
L1.NZD        -0.038639         0.044523           -0.868           0.386
L1.EUR         1.069417         0.030502           35.061           0.000
L2.AUD         0.068170         0.056730            1.202           0.230
L2.CHF         0.044109         0.043845            1.006           0.314
L2.JPY        -2.286347         3.377677           -0.677           0.499
L2.GBP        -0.007655         0.028205           -0.271           0.786
L2.NZD         0.073476         0.062832            1.169           0.242
L2.EUR        -0.094016         0.044059           -2.134           0.033
L3.AUD        -0.000228         0.056777           -0.004           0.997
L3.CHF        -0.015888         0.044070           -0.361           0.718
L3.JPY         5.101234         3.385234            1.507           0.132
L3.GBP         0.006476         0.028274            0.229           0.819
L3.NZD        -0.002670         0.062822           -0.043           0.966
L3.EUR        -0.032445         0.044146           -0.735           0.462
L4.AUD         0.002425         0.056831            0.043           0.966
L4.CHF        -0.050316         0.043700           -1.151           0.250
L4.JPY        -3.159527         3.384778           -0.933           0.351
L4.GBP        -0.032376         0.028276           -1.145           0.252
L4.NZD        -0.083055         0.062781           -1.323           0.186
L4.EUR         0.144009         0.043952            3.277           0.001
L5.AUD         0.018162         0.040373            0.450           0.653
L5.CHF         0.043818         0.029360            1.492           0.136
L5.JPY        -1.179790         2.422530           -0.487           0.626
L5.GBP         0.022682         0.019660            1.154           0.249
L5.NZD         0.045797         0.044470            1.030           0.303
L5.EUR        -0.090112         0.030373           -2.967           0.003
=========================================================================

Correlation matrix of residuals
            AUD       CHF       JPY       GBP       NZD       EUR
AUD    1.000000  0.366213 -0.014603  0.576073  0.837977  0.612364
CHF    0.366213  1.000000  0.339627  0.449909  0.394722  0.683484
JPY   -0.014603  0.339627  1.000000  0.070887  0.025442  0.190721
GBP    0.576073  0.449909  0.070887  1.000000  0.560841  0.656645
NZD    0.837977  0.394722  0.025442  0.560841  1.000000  0.595447
EUR    0.612364  0.683484  0.190721  0.656645  0.595447  1.000000

In [41]:
res.plot()
In [43]:
res.plot_forecast(40)
In [ ]: